//
//  GPSRecordingViewController.m
//  dataGathering
//
//  Created by macmini on 15/2/2.
//  Copyright (c) 2015年 shuili. All rights reserved.
//

#import "GPSRecordingViewController.h"
#import "GPSRecordingTableViewCell.h"

@interface GPSRecordingViewController ()

@end

@implementation GPSRecordingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    ((UITableViewController*)[[[[self childViewControllers] objectAtIndex:0] childViewControllers] objectAtIndex:0]).tableView.dataSource = self;
    ((UITableViewController*)[[[[self childViewControllers] objectAtIndex:0] childViewControllers] objectAtIndex:0]).tableView.delegate = self;
    
    [(UIButton*)[[[[self childViewControllers] objectAtIndex:0] view] viewWithTag:2] addTarget:self action:@selector(finishRecording) forControlEvents:UIControlEventTouchUpInside];

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(!_geometry)
    {
        return 0;
    }
    if([[_recordingType uppercaseString] isEqualToString:@"POLYLINE"]){
        return [(AGSPolyline*)_geometry numPoints];
    } else if([[_recordingType uppercaseString] isEqualToString:@"POLYGON"]){
        return [(AGSPolygon*)_geometry numPoints];
    } else {
        return 1;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 48.0;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    GPSRecordingTableViewCell *cell;
    
    static  NSString *identifier = @ "gpsRecordingTableViewCell" ;

    cell = (GPSRecordingTableViewCell*)[tableView dequeueReusableCellWithIdentifier:identifier];

    if  (!cell) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@ "GPSRecordingTableViewCell"  owner:nil options:nil];
        cell = [nib objectAtIndex:0];
    }
    
    NSLog(@"to display geometry point NO is %d",[((AGSPolygon*)_geometry) numPoints]);
    NSString *timeLabel = [NSString stringWithFormat:@"%@的轨迹",[_timeArray objectAtIndex:[indexPath row]]];
    AGSPoint *p;
    if([[_recordingType uppercaseString] isEqualToString:@"POLYLINE"]){
        p = [((AGSPolyline*)_geometry) pointOnPath:0 atIndex:[indexPath row]];
    } else if([[_recordingType uppercaseString] isEqualToString:@"POLYGON"]){
        p = [((AGSPolygon*)_geometry) pointOnRing:0 atIndex:[indexPath row]];
    } else {
        p = (AGSPoint*)_geometry;
    }
    NSString *coordinateString = [NSString stringWithFormat:@"X=%.5f  Y=%.5f",[p x],[p y]];
    
    cell.mainLabelString = timeLabel;
    cell.coordinateLabelString = coordinateString;
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setBackgroundImage:[UIImage imageNamed:@"u610.png"] forState:UIControlStateNormal];
    [button setFrame:CGRectMake(416, 8, 32, 32)];
    [button addTarget:self action:@selector(del:) forControlEvents:UIControlEventTouchUpInside];
    [cell.contentView addSubview:button];
    
    cell.tag = [indexPath row];
    
    NSArray *subviews = [cell.contentView subviews];
    for(id view in subviews)
    {
        if([view isKindOfClass:[UIButton class]])
        {
            [view setTag:[indexPath row]];
            [cell.contentView bringSubviewToFront:view];
        }
    }
    
    return cell;
}

-(void)del:(UIButton *)button
{
    NSArray *visiblecells = [((UITableViewController*)[[[[self childViewControllers] objectAtIndex:0] childViewControllers] objectAtIndex:0]).tableView visibleCells];
    for(UITableViewCell *cell in visiblecells)
    {
        if(cell.tag == button.tag)
        {
            [((AGSMutablePolygon*)_geometry) removePointOnRing:0 atIndex:[cell tag]];
            [_timeArray removeObjectAtIndex:[cell tag]];
            //[array removeObjectAtIndex:[cell tag]];
            [((UITableViewController*)[[[[self childViewControllers] objectAtIndex:0] childViewControllers] objectAtIndex:0]).tableView reloadData];
            break;
        }
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)reloadTableView
{
    [((UITableViewController*)[[[[self childViewControllers] objectAtIndex:0] childViewControllers] objectAtIndex:0]).tableView reloadData];
    
    if([((UITableViewController*)[[[[self childViewControllers] objectAtIndex:0] childViewControllers] objectAtIndex:0]).tableView numberOfRowsInSection:0] > 10){
    
        [((UITableViewController*)[[[[self childViewControllers] objectAtIndex:0] childViewControllers] objectAtIndex:0]).tableView setContentOffset:CGPointMake(0, ((UITableViewController*)[[[[self childViewControllers] objectAtIndex:0] childViewControllers] objectAtIndex:0]).tableView.contentSize.height -((UITableViewController*)[[[[self childViewControllers] objectAtIndex:0] childViewControllers] objectAtIndex:0]).tableView.bounds.size.height) animated:NO];
    }
}

-(void)finishRecording
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"finishRecordingLocation" object:nil];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
